blob: 369a8e38e3e2ec6077a974571d381948202b4657 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Mike Frysinger078f2f12011-05-14 06:56:15 +000014#include <linux/compiler.h>
Marek Vasut0e1b5772012-10-06 14:07:03 +000015#include <errno.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
wdenk7ac16102004-08-01 22:48:16 +000017
Wolfgang Denk6405a152006-03-31 18:32:53 +020018DECLARE_GLOBAL_DATA_PTR;
19
Gerlando Falauto34148d62011-11-18 06:49:11 +000020static struct serial_device *serial_devices;
21static struct serial_device *serial_current;
Joe Hershberger3928bb62012-12-11 22:16:27 -060022/*
23 * Table with supported baudrates (defined in config_xyz.h)
24 */
Tom Rini6a5dccc2022-11-16 13:10:41 -050025static const unsigned long baudrate_table[] = CFG_SYS_BAUDRATE_TABLE;
wdenk7ac16102004-08-01 22:48:16 +000026
Marek Vasut95656ea2012-10-08 11:36:39 +000027/**
28 * serial_null() - Void registration routine of a serial driver
29 *
30 * This routine implements a void registration routine of a serial
31 * driver. The registration routine of a particular driver is aliased
32 * to this empty function in case the driver is not compiled into
33 * U-Boot.
34 */
Marek Vasutf6af0482012-09-12 17:49:58 +020035static void serial_null(void)
36{
Joe Hershberger3928bb62012-12-11 22:16:27 -060037}
38
39/**
40 * on_baudrate() - Update the actual baudrate when the env var changes
41 *
Heinrich Schuchardt995ea732018-07-29 10:41:02 +020042 * @name: changed environment variable
43 * @value: new value of the environment variable
44 * @op: operation (create, overwrite, or delete)
45 * @flags: attributes of environment variable change,
46 * see flags H_* in include/search.h
47 *
Joe Hershberger3928bb62012-12-11 22:16:27 -060048 * This will check for a valid baudrate and only apply it if valid.
Heinrich Schuchardt995ea732018-07-29 10:41:02 +020049 *
50 * Return: 0 on success, 1 on error
Joe Hershberger3928bb62012-12-11 22:16:27 -060051 */
52static int on_baudrate(const char *name, const char *value, enum env_op op,
53 int flags)
54{
55 int i;
56 int baudrate;
57
58 switch (op) {
59 case env_op_create:
60 case env_op_overwrite:
61 /*
62 * Switch to new baudrate if new baudrate is supported
63 */
Simon Glassff9b9032021-07-24 09:03:30 -060064 baudrate = dectoul(value, NULL);
Joe Hershberger3928bb62012-12-11 22:16:27 -060065
66 /* Not actually changing */
67 if (gd->baudrate == baudrate)
68 return 0;
69
Axel Linebca8732013-06-23 00:46:41 +080070 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
Joe Hershberger3928bb62012-12-11 22:16:27 -060071 if (baudrate == baudrate_table[i])
72 break;
73 }
Axel Linebca8732013-06-23 00:46:41 +080074 if (i == ARRAY_SIZE(baudrate_table)) {
Joe Hershberger3928bb62012-12-11 22:16:27 -060075 if ((flags & H_FORCE) == 0)
76 printf("## Baudrate %d bps not supported\n",
77 baudrate);
78 return 1;
79 }
80 if ((flags & H_INTERACTIVE) != 0) {
81 printf("## Switch baudrate to %d"
82 " bps and press ENTER ...\n", baudrate);
83 udelay(50000);
84 }
85
86 gd->baudrate = baudrate;
Joe Hershberger3928bb62012-12-11 22:16:27 -060087
88 serial_setbrg();
89
90 udelay(50000);
91
92 if ((flags & H_INTERACTIVE) != 0)
93 while (1) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +020094 if (getchar() == '\r')
Joe Hershberger3928bb62012-12-11 22:16:27 -060095 break;
96 }
97
98 return 0;
99 case env_op_delete:
100 printf("## Baudrate may not be deleted\n");
101 return 1;
102 default:
103 return 0;
104 }
Marek Vasutf6af0482012-09-12 17:49:58 +0200105}
Joe Hershberger3928bb62012-12-11 22:16:27 -0600106U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
Marek Vasutf6af0482012-09-12 17:49:58 +0200107
Marek Vasut95656ea2012-10-08 11:36:39 +0000108/**
109 * serial_initfunc() - Forward declare of driver registration routine
110 * @name: Name of the real driver registration routine.
111 *
112 * This macro expands onto forward declaration of a driver registration
113 * routine, which is then used below in serial_initialize() function.
114 * The declaration is made weak and aliases to serial_null() so in case
115 * the driver is not compiled in, the function is still declared and can
116 * be used, but aliases to serial_null() and thus is optimized away.
117 */
Marek Vasutf6af0482012-09-12 17:49:58 +0200118#define serial_initfunc(name) \
119 void name(void) \
120 __attribute__((weak, alias("serial_null")));
121
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100122serial_initfunc(atmel_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100123serial_initfunc(mcf_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100124serial_initfunc(mpc85xx_serial_initialize);
Marek Vasut64c60552012-09-14 22:37:43 +0200125serial_initfunc(mxc_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100126serial_initfunc(ns16550_serial_initialize);
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200127serial_initfunc(pl01x_serial_initialize);
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100128serial_initfunc(pxa_serial_initialize);
Sean Andersond4f131a2022-03-22 16:59:24 -0400129serial_initfunc(smh_serial_initialize);
Marek Vasut904d3d72012-09-14 22:40:08 +0200130serial_initfunc(sh_serial_initialize);
developer77c7c732019-09-25 17:45:18 +0800131serial_initfunc(mtk_serial_initialize);
Marek Vasut419d8942012-09-12 13:50:56 +0200132
Marek Vasut95656ea2012-10-08 11:36:39 +0000133/**
134 * serial_register() - Register serial driver with serial driver core
135 * @dev: Pointer to the serial driver structure
136 *
137 * This function registers the serial driver supplied via @dev with
138 * serial driver core, thus making U-Boot aware of it and making it
139 * available for U-Boot to use. On platforms that still require manual
140 * relocation of constant variables, relocation of the supplied structure
141 * is performed.
142 */
Mike Frysingerffadc822011-04-29 18:03:30 +0000143void serial_register(struct serial_device *dev)
wdenk7ac16102004-08-01 22:48:16 +0000144{
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200145#ifdef CONFIG_NEEDS_MANUAL_RELOC
Marek Vasut2206b342012-09-16 18:54:22 +0200146 if (dev->start)
147 dev->start += gd->reloc_off;
148 if (dev->stop)
149 dev->stop += gd->reloc_off;
150 if (dev->setbrg)
151 dev->setbrg += gd->reloc_off;
152 if (dev->getc)
153 dev->getc += gd->reloc_off;
154 if (dev->tstc)
155 dev->tstc += gd->reloc_off;
156 if (dev->putc)
157 dev->putc += gd->reloc_off;
158 if (dev->puts)
159 dev->puts += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500160#endif
wdenk7ac16102004-08-01 22:48:16 +0000161
162 dev->next = serial_devices;
163 serial_devices = dev;
wdenk7ac16102004-08-01 22:48:16 +0000164}
165
Marek Vasut95656ea2012-10-08 11:36:39 +0000166/**
167 * serial_initialize() - Register all compiled-in serial port drivers
168 *
169 * This function registers all serial port drivers that are compiled
170 * into the U-Boot binary with the serial core, thus making them
171 * available to U-Boot to use. Lastly, this function assigns a default
172 * serial port to the serial core. That serial port is then used as a
173 * default output.
174 */
Ovidiu Panait96008c92020-07-24 14:12:22 +0300175int serial_initialize(void)
wdenk7ac16102004-08-01 22:48:16 +0000176{
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100177 atmel_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100178 mcf_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100179 mpc85xx_serial_initialize();
Marek Vasut64c60552012-09-14 22:37:43 +0200180 mxc_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100181 ns16550_serial_initialize();
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200182 pl01x_serial_initialize();
Jeroen Hofstee6512cc12014-10-27 20:10:07 +0100183 pxa_serial_initialize();
Sean Andersond4f131a2022-03-22 16:59:24 -0400184 smh_serial_initialize();
Marek Vasut904d3d72012-09-14 22:40:08 +0200185 sh_serial_initialize();
developer77c7c732019-09-25 17:45:18 +0800186 mtk_serial_initialize();
Marek Vasut01f9ea82012-09-13 01:16:50 +0200187
Gerlando Falauto34148d62011-11-18 06:49:11 +0000188 serial_assign(default_serial_console()->name);
Ovidiu Panait96008c92020-07-24 14:12:22 +0300189
190 return 0;
wdenk7ac16102004-08-01 22:48:16 +0000191}
192
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200193static int serial_stub_start(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600194{
195 struct serial_device *dev = sdev->priv;
196
197 return dev->start();
198}
199
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200200static int serial_stub_stop(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600201{
202 struct serial_device *dev = sdev->priv;
203
204 return dev->stop();
205}
206
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200207static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600208{
209 struct serial_device *dev = sdev->priv;
210
211 dev->putc(ch);
212}
213
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200214static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600215{
216 struct serial_device *dev = sdev->priv;
217
218 dev->puts(str);
219}
220
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900221static int serial_stub_getc(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600222{
223 struct serial_device *dev = sdev->priv;
224
225 return dev->getc();
226}
227
Masahiro Yamada5e0ef842017-06-22 16:48:49 +0900228static int serial_stub_tstc(struct stdio_dev *sdev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600229{
230 struct serial_device *dev = sdev->priv;
231
232 return dev->tstc();
233}
234
Marek Vasut95656ea2012-10-08 11:36:39 +0000235/**
236 * serial_stdio_init() - Register serial ports with STDIO core
237 *
238 * This function generates a proxy driver for each serial port driver.
239 * These proxy drivers then register with the STDIO core, making the
240 * serial drivers available as STDIO devices.
241 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000242void serial_stdio_init(void)
wdenk7ac16102004-08-01 22:48:16 +0000243{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200244 struct stdio_dev dev;
wdenk7ac16102004-08-01 22:48:16 +0000245 struct serial_device *s = serial_devices;
246
wdenk8f08c5b2004-10-11 22:43:02 +0000247 while (s) {
Gerlando Falauto34148d62011-11-18 06:49:11 +0000248 memset(&dev, 0, sizeof(dev));
wdenk7ac16102004-08-01 22:48:16 +0000249
Gerlando Falauto34148d62011-11-18 06:49:11 +0000250 strcpy(dev.name, s->name);
wdenk7ac16102004-08-01 22:48:16 +0000251 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
252
Simon Glass0d1e1f72014-07-23 06:54:59 -0600253 dev.start = serial_stub_start;
254 dev.stop = serial_stub_stop;
255 dev.putc = serial_stub_putc;
256 dev.puts = serial_stub_puts;
257 dev.getc = serial_stub_getc;
258 dev.tstc = serial_stub_tstc;
Simon Glassac2a14f2014-09-04 16:27:23 -0600259 dev.priv = s;
wdenk7ac16102004-08-01 22:48:16 +0000260
Gerlando Falauto34148d62011-11-18 06:49:11 +0000261 stdio_register(&dev);
wdenk7ac16102004-08-01 22:48:16 +0000262
263 s = s->next;
264 }
265}
266
Marek Vasut95656ea2012-10-08 11:36:39 +0000267/**
268 * serial_assign() - Select the serial output device by name
269 * @name: Name of the serial driver to be used as default output
270 *
271 * This function configures the serial output multiplexing by
272 * selecting which serial device will be used as default. In case
273 * the STDIO "serial" device is selected as stdin/stdout/stderr,
274 * the serial device previously configured by this function will be
275 * used for the particular operation.
276 *
277 * Returns 0 on success, negative on error.
278 */
Gerlando Falauto92999582011-11-18 06:49:12 +0000279int serial_assign(const char *name)
wdenk7ac16102004-08-01 22:48:16 +0000280{
281 struct serial_device *s;
282
wdenk8f08c5b2004-10-11 22:43:02 +0000283 for (s = serial_devices; s; s = s->next) {
Marek Vasut0e1b5772012-10-06 14:07:03 +0000284 if (strcmp(s->name, name))
285 continue;
286 serial_current = s;
287 return 0;
wdenk7ac16102004-08-01 22:48:16 +0000288 }
289
Marek Vasut0e1b5772012-10-06 14:07:03 +0000290 return -EINVAL;
wdenk7ac16102004-08-01 22:48:16 +0000291}
292
Marek Vasut95656ea2012-10-08 11:36:39 +0000293/**
294 * serial_reinit_all() - Reinitialize all compiled-in serial ports
295 *
296 * This function reinitializes all serial ports that are compiled
297 * into U-Boot by calling their serial_start() functions.
298 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000299void serial_reinit_all(void)
wdenk7ac16102004-08-01 22:48:16 +0000300{
301 struct serial_device *s;
302
Gerlando Falauto34148d62011-11-18 06:49:11 +0000303 for (s = serial_devices; s; s = s->next)
Marek Vasutb46931d2012-09-07 14:35:31 +0200304 s->start();
wdenk7ac16102004-08-01 22:48:16 +0000305}
306
Marek Vasut95656ea2012-10-08 11:36:39 +0000307/**
308 * get_current() - Return pointer to currently selected serial port
309 *
310 * This function returns a pointer to currently selected serial port.
311 * The currently selected serial port is altered by serial_assign()
312 * function.
313 *
314 * In case this function is called before relocation or before any serial
315 * port is configured, this function calls default_serial_console() to
316 * determine the serial port. Otherwise, the configured serial port is
317 * returned.
318 *
319 * Returns pointer to the currently selected serial port on success,
320 * NULL on error.
321 */
Simon Glass8f4e6f12011-08-19 11:09:43 +0000322static struct serial_device *get_current(void)
wdenk7ac16102004-08-01 22:48:16 +0000323{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000324 struct serial_device *dev;
325
Marek Vasutbdad8072012-10-06 14:07:04 +0000326 if (!(gd->flags & GD_FLG_RELOC))
Simon Glass8f4e6f12011-08-19 11:09:43 +0000327 dev = default_serial_console();
Marek Vasutbdad8072012-10-06 14:07:04 +0000328 else if (!serial_current)
329 dev = default_serial_console();
330 else
331 dev = serial_current;
wdenk8f08c5b2004-10-11 22:43:02 +0000332
Marek Vasutbdad8072012-10-06 14:07:04 +0000333 /* We must have a console device */
334 if (!dev) {
Marek Vasuta6f63342012-09-15 10:33:51 +0200335#ifdef CONFIG_SPL_BUILD
Marek Vasutbdad8072012-10-06 14:07:04 +0000336 puts("Cannot find console\n");
337 hang();
Marek Vasuta6f63342012-09-15 10:33:51 +0200338#else
Marek Vasutbdad8072012-10-06 14:07:04 +0000339 panic("Cannot find console\n");
Marek Vasuta6f63342012-09-15 10:33:51 +0200340#endif
Marek Vasutbdad8072012-10-06 14:07:04 +0000341 }
342
Simon Glass8f4e6f12011-08-19 11:09:43 +0000343 return dev;
344}
wdenk7ac16102004-08-01 22:48:16 +0000345
Marek Vasut95656ea2012-10-08 11:36:39 +0000346/**
347 * serial_init() - Initialize currently selected serial port
348 *
349 * This function initializes the currently selected serial port. This
350 * usually involves setting up the registers of that particular port,
351 * enabling clock and such. This function uses the get_current() call
352 * to determine which port is selected.
353 *
354 * Returns 0 on success, negative on error.
355 */
Simon Glass8f4e6f12011-08-19 11:09:43 +0000356int serial_init(void)
357{
Simon Glassb9d5f6b2014-07-23 06:55:07 -0600358 gd->flags |= GD_FLG_SERIAL_READY;
Marek Vasutb46931d2012-09-07 14:35:31 +0200359 return get_current()->start();
wdenk7ac16102004-08-01 22:48:16 +0000360}
361
Marek Vasut95656ea2012-10-08 11:36:39 +0000362/**
363 * serial_setbrg() - Configure baud-rate of currently selected serial port
364 *
365 * This function configures the baud-rate of the currently selected
366 * serial port. The baud-rate is retrieved from global data within
367 * the serial port driver. This function uses the get_current() call
368 * to determine which port is selected.
369 *
370 * Returns 0 on success, negative on error.
371 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000372void serial_setbrg(void)
wdenk7ac16102004-08-01 22:48:16 +0000373{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000374 get_current()->setbrg();
wdenk7ac16102004-08-01 22:48:16 +0000375}
376
Marek Vasut95656ea2012-10-08 11:36:39 +0000377/**
378 * serial_getc() - Read character from currently selected serial port
379 *
380 * This function retrieves a character from currently selected serial
381 * port. In case there is no character waiting on the serial port,
382 * this function will block and wait for the character to appear. This
383 * function uses the get_current() call to determine which port is
384 * selected.
385 *
386 * Returns the character on success, negative on error.
387 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000388int serial_getc(void)
wdenk7ac16102004-08-01 22:48:16 +0000389{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000390 return get_current()->getc();
wdenk7ac16102004-08-01 22:48:16 +0000391}
392
Marek Vasut95656ea2012-10-08 11:36:39 +0000393/**
394 * serial_tstc() - Test if data is available on currently selected serial port
395 *
396 * This function tests if one or more characters are available on
397 * currently selected serial port. This function never blocks. This
398 * function uses the get_current() call to determine which port is
399 * selected.
400 *
401 * Returns positive if character is available, zero otherwise.
402 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000403int serial_tstc(void)
wdenk7ac16102004-08-01 22:48:16 +0000404{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000405 return get_current()->tstc();
wdenk7ac16102004-08-01 22:48:16 +0000406}
407
Marek Vasut95656ea2012-10-08 11:36:39 +0000408/**
409 * serial_putc() - Output character via currently selected serial port
410 * @c: Single character to be output from the serial port.
411 *
412 * This function outputs a character via currently selected serial
413 * port. This character is passed to the serial port driver responsible
414 * for controlling the hardware. The hardware may still be in process
415 * of transmitting another character, therefore this function may block
416 * for a short amount of time. This function uses the get_current()
417 * call to determine which port is selected.
418 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000419void serial_putc(const char c)
wdenk7ac16102004-08-01 22:48:16 +0000420{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000421 get_current()->putc(c);
wdenk7ac16102004-08-01 22:48:16 +0000422}
423
Marek Vasut95656ea2012-10-08 11:36:39 +0000424/**
425 * serial_puts() - Output string via currently selected serial port
426 * @s: Zero-terminated string to be output from the serial port.
427 *
428 * This function outputs a zero-terminated string via currently
429 * selected serial port. This function behaves as an accelerator
430 * in case the hardware can queue multiple characters for transfer.
431 * The whole string that is to be output is available to the function
432 * implementing the hardware manipulation. Transmitting the whole
433 * string may take some time, thus this function may block for some
434 * amount of time. This function uses the get_current() call to
435 * determine which port is selected.
436 */
Gerlando Falauto34148d62011-11-18 06:49:11 +0000437void serial_puts(const char *s)
wdenk7ac16102004-08-01 22:48:16 +0000438{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000439 get_current()->puts(s);
wdenk7ac16102004-08-01 22:48:16 +0000440}
Mike Frysinger078f2f12011-05-14 06:56:15 +0000441
Marek Vasut95656ea2012-10-08 11:36:39 +0000442/**
443 * default_serial_puts() - Output string by calling serial_putc() in loop
444 * @s: Zero-terminated string to be output from the serial port.
445 *
446 * This function outputs a zero-terminated string by calling serial_putc()
447 * in a loop. Most drivers do not support queueing more than one byte for
448 * transfer, thus this function precisely implements their serial_puts().
449 *
450 * To optimize the number of get_current() calls, this function only
451 * calls get_current() once and then directly accesses the putc() call
452 * of the &struct serial_device .
453 */
Marek Vasut6b9124e2012-10-06 14:07:01 +0000454void default_serial_puts(const char *s)
455{
456 struct serial_device *dev = get_current();
457 while (*s)
458 dev->putc(*s++);
459}
460
Tom Rini8eaa3c72022-11-19 18:45:44 -0500461#if CFG_POST & CONFIG_SYS_POST_UART
Tom Rini6a5dccc2022-11-16 13:10:41 -0500462static const int bauds[] = CFG_SYS_BAUDRATE_TABLE;
Mike Frysinger078f2f12011-05-14 06:56:15 +0000463
Marek Vasut95656ea2012-10-08 11:36:39 +0000464/**
465 * uart_post_test() - Test the currently selected serial port using POST
466 * @flags: POST framework flags
467 *
468 * Do a loopback test of the currently selected serial port. This
469 * function is only useful in the context of the POST testing framwork.
Vagrant Cascadianbeb288b2015-11-24 14:46:24 -0800470 * The serial port is first configured into loopback mode and then
Marek Vasut95656ea2012-10-08 11:36:39 +0000471 * characters are sent through it.
472 *
473 * Returns 0 on success, value otherwise.
474 */
Mike Frysinger078f2f12011-05-14 06:56:15 +0000475/* Mark weak until post/cpu/.../uart.c migrate over */
476__weak
477int uart_post_test(int flags)
478{
479 unsigned char c;
480 int ret, saved_baud, b;
481 struct serial_device *saved_dev, *s;
Mike Frysinger078f2f12011-05-14 06:56:15 +0000482
483 /* Save current serial state */
484 ret = 0;
485 saved_dev = serial_current;
Masahiro Yamada197c7202014-04-04 20:09:58 +0900486 saved_baud = gd->baudrate;
Mike Frysinger078f2f12011-05-14 06:56:15 +0000487
488 for (s = serial_devices; s; s = s->next) {
489 /* If this driver doesn't support loop back, skip it */
490 if (!s->loop)
491 continue;
492
493 /* Test the next device */
494 serial_current = s;
495
496 ret = serial_init();
497 if (ret)
498 goto done;
499
500 /* Consume anything that happens to be queued */
501 while (serial_tstc())
502 serial_getc();
503
504 /* Enable loop back */
505 s->loop(1);
506
507 /* Test every available baud rate */
508 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
Masahiro Yamada197c7202014-04-04 20:09:58 +0900509 gd->baudrate = bauds[b];
Mike Frysinger078f2f12011-05-14 06:56:15 +0000510 serial_setbrg();
511
512 /*
513 * Stick to printable chars to avoid issues:
514 * - terminal corruption
515 * - serial program reacting to sequences and sending
516 * back random extra data
517 * - most serial drivers add in extra chars (like \r\n)
518 */
519 for (c = 0x20; c < 0x7f; ++c) {
520 /* Send it out */
521 serial_putc(c);
522
523 /* Make sure it's the same one */
524 ret = (c != serial_getc());
525 if (ret) {
526 s->loop(0);
527 goto done;
528 }
529
530 /* Clean up the output in case it was sent */
531 serial_putc('\b');
532 ret = ('\b' != serial_getc());
533 if (ret) {
534 s->loop(0);
535 goto done;
536 }
537 }
538 }
539
540 /* Disable loop back */
541 s->loop(0);
542
Marek Vasutb46931d2012-09-07 14:35:31 +0200543 /* XXX: There is no serial_stop() !? */
544 if (s->stop)
545 s->stop();
Mike Frysinger078f2f12011-05-14 06:56:15 +0000546 }
547
548 done:
549 /* Restore previous serial state */
550 serial_current = saved_dev;
Masahiro Yamada197c7202014-04-04 20:09:58 +0900551 gd->baudrate = saved_baud;
Mike Frysinger078f2f12011-05-14 06:56:15 +0000552 serial_reinit_all();
553 serial_setbrg();
554
555 return ret;
556}
557#endif