1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 #include <env_internal.h>
11 #include <stdio_dev.h>
13 #include <linux/compiler.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 static struct serial_device *serial_devices;
19 static struct serial_device *serial_current;
21 * Table with supported baudrates (defined in config_xyz.h)
23 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
26 * serial_null() - Void registration routine of a serial driver
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
33 static void serial_null(void)
38 * on_baudrate() - Update the actual baudrate when the env var changes
40 * @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
46 * This will check for a valid baudrate and only apply it if valid.
48 * Return: 0 on success, 1 on error
50 static int on_baudrate(const char *name, const char *value, enum env_op op,
58 case env_op_overwrite:
60 * Switch to new baudrate if new baudrate is supported
62 baudrate = simple_strtoul(value, NULL, 10);
64 /* Not actually changing */
65 if (gd->baudrate == baudrate)
68 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
69 if (baudrate == baudrate_table[i])
72 if (i == ARRAY_SIZE(baudrate_table)) {
73 if ((flags & H_FORCE) == 0)
74 printf("## Baudrate %d bps not supported\n",
78 if ((flags & H_INTERACTIVE) != 0) {
79 printf("## Switch baudrate to %d"
80 " bps and press ENTER ...\n", baudrate);
84 gd->baudrate = baudrate;
90 if ((flags & H_INTERACTIVE) != 0)
98 printf("## Baudrate may not be deleted\n");
104 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
107 * serial_initfunc() - Forward declare of driver registration routine
108 * @name: Name of the real driver registration routine.
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.
116 #define serial_initfunc(name) \
118 __attribute__((weak, alias("serial_null")));
120 serial_initfunc(atmel_serial_initialize);
121 serial_initfunc(mcf_serial_initialize);
122 serial_initfunc(mpc85xx_serial_initialize);
123 serial_initfunc(mxc_serial_initialize);
124 serial_initfunc(ns16550_serial_initialize);
125 serial_initfunc(pl01x_serial_initialize);
126 serial_initfunc(pxa_serial_initialize);
127 serial_initfunc(sh_serial_initialize);
128 serial_initfunc(mtk_serial_initialize);
131 * serial_register() - Register serial driver with serial driver core
132 * @dev: Pointer to the serial driver structure
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
140 void serial_register(struct serial_device *dev)
142 #ifdef CONFIG_NEEDS_MANUAL_RELOC
144 dev->start += gd->reloc_off;
146 dev->stop += gd->reloc_off;
148 dev->setbrg += gd->reloc_off;
150 dev->getc += gd->reloc_off;
152 dev->tstc += gd->reloc_off;
154 dev->putc += gd->reloc_off;
156 dev->puts += gd->reloc_off;
159 dev->next = serial_devices;
160 serial_devices = dev;
164 * serial_initialize() - Register all compiled-in serial port drivers
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
172 void serial_initialize(void)
174 atmel_serial_initialize();
175 mcf_serial_initialize();
176 mpc85xx_serial_initialize();
177 mxc_serial_initialize();
178 ns16550_serial_initialize();
179 pl01x_serial_initialize();
180 pxa_serial_initialize();
181 sh_serial_initialize();
182 mtk_serial_initialize();
184 serial_assign(default_serial_console()->name);
187 static int serial_stub_start(struct stdio_dev *sdev)
189 struct serial_device *dev = sdev->priv;
194 static int serial_stub_stop(struct stdio_dev *sdev)
196 struct serial_device *dev = sdev->priv;
201 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
203 struct serial_device *dev = sdev->priv;
208 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
210 struct serial_device *dev = sdev->priv;
215 static int serial_stub_getc(struct stdio_dev *sdev)
217 struct serial_device *dev = sdev->priv;
222 static int serial_stub_tstc(struct stdio_dev *sdev)
224 struct serial_device *dev = sdev->priv;
230 * serial_stdio_init() - Register serial ports with STDIO core
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.
236 void serial_stdio_init(void)
238 struct stdio_dev dev;
239 struct serial_device *s = serial_devices;
242 memset(&dev, 0, sizeof(dev));
244 strcpy(dev.name, s->name);
245 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
247 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;
255 stdio_register(&dev);
262 * serial_assign() - Select the serial output device by name
263 * @name: Name of the serial driver to be used as default output
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.
271 * Returns 0 on success, negative on error.
273 int serial_assign(const char *name)
275 struct serial_device *s;
277 for (s = serial_devices; s; s = s->next) {
278 if (strcmp(s->name, name))
288 * serial_reinit_all() - Reinitialize all compiled-in serial ports
290 * This function reinitializes all serial ports that are compiled
291 * into U-Boot by calling their serial_start() functions.
293 void serial_reinit_all(void)
295 struct serial_device *s;
297 for (s = serial_devices; s; s = s->next)
302 * get_current() - Return pointer to currently selected serial port
304 * This function returns a pointer to currently selected serial port.
305 * The currently selected serial port is altered by serial_assign()
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
313 * Returns pointer to the currently selected serial port on success,
316 static struct serial_device *get_current(void)
318 struct serial_device *dev;
320 if (!(gd->flags & GD_FLG_RELOC))
321 dev = default_serial_console();
322 else if (!serial_current)
323 dev = default_serial_console();
325 dev = serial_current;
327 /* We must have a console device */
329 #ifdef CONFIG_SPL_BUILD
330 puts("Cannot find console\n");
333 panic("Cannot find console\n");
341 * serial_init() - Initialize currently selected serial port
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.
348 * Returns 0 on success, negative on error.
350 int serial_init(void)
352 gd->flags |= GD_FLG_SERIAL_READY;
353 return get_current()->start();
357 * serial_setbrg() - Configure baud-rate of currently selected serial port
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.
364 * Returns 0 on success, negative on error.
366 void serial_setbrg(void)
368 get_current()->setbrg();
372 * serial_getc() - Read character from currently selected serial port
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
380 * Returns the character on success, negative on error.
382 int serial_getc(void)
384 return get_current()->getc();
388 * serial_tstc() - Test if data is available on currently selected serial port
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
395 * Returns positive if character is available, zero otherwise.
397 int serial_tstc(void)
399 return get_current()->tstc();
403 * serial_putc() - Output character via currently selected serial port
404 * @c: Single character to be output from the serial port.
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.
413 void serial_putc(const char c)
415 get_current()->putc(c);
419 * serial_puts() - Output string via currently selected serial port
420 * @s: Zero-terminated string to be output from the serial port.
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.
431 void serial_puts(const char *s)
433 get_current()->puts(s);
437 * default_serial_puts() - Output string by calling serial_putc() in loop
438 * @s: Zero-terminated string to be output from the serial port.
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().
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 .
448 void default_serial_puts(const char *s)
450 struct serial_device *dev = get_current();
455 #if CONFIG_POST & CONFIG_SYS_POST_UART
456 static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
459 * uart_post_test() - Test the currently selected serial port using POST
460 * @flags: POST framework flags
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.
464 * The serial port is first configured into loopback mode and then
465 * characters are sent through it.
467 * Returns 0 on success, value otherwise.
469 /* Mark weak until post/cpu/.../uart.c migrate over */
471 int uart_post_test(int flags)
474 int ret, saved_baud, b;
475 struct serial_device *saved_dev, *s;
477 /* Save current serial state */
479 saved_dev = serial_current;
480 saved_baud = gd->baudrate;
482 for (s = serial_devices; s; s = s->next) {
483 /* If this driver doesn't support loop back, skip it */
487 /* Test the next device */
494 /* Consume anything that happens to be queued */
495 while (serial_tstc())
498 /* Enable loop back */
501 /* Test every available baud rate */
502 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
503 gd->baudrate = bauds[b];
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)
513 for (c = 0x20; c < 0x7f; ++c) {
517 /* Make sure it's the same one */
518 ret = (c != serial_getc());
524 /* Clean up the output in case it was sent */
526 ret = ('\b' != serial_getc());
534 /* Disable loop back */
537 /* XXX: There is no serial_stop() !? */
543 /* Restore previous serial state */
544 serial_current = saved_dev;
545 gd->baudrate = saved_baud;