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>
15 #include <linux/delay.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 static struct serial_device *serial_devices;
20 static struct serial_device *serial_current;
22 * Table with supported baudrates (defined in config_xyz.h)
24 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
27 * serial_null() - Void registration routine of a serial driver
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
34 static void serial_null(void)
39 * on_baudrate() - Update the actual baudrate when the env var changes
41 * @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
47 * This will check for a valid baudrate and only apply it if valid.
49 * Return: 0 on success, 1 on error
51 static int on_baudrate(const char *name, const char *value, enum env_op op,
59 case env_op_overwrite:
61 * Switch to new baudrate if new baudrate is supported
63 baudrate = simple_strtoul(value, NULL, 10);
65 /* Not actually changing */
66 if (gd->baudrate == baudrate)
69 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
70 if (baudrate == baudrate_table[i])
73 if (i == ARRAY_SIZE(baudrate_table)) {
74 if ((flags & H_FORCE) == 0)
75 printf("## Baudrate %d bps not supported\n",
79 if ((flags & H_INTERACTIVE) != 0) {
80 printf("## Switch baudrate to %d"
81 " bps and press ENTER ...\n", baudrate);
85 gd->baudrate = baudrate;
91 if ((flags & H_INTERACTIVE) != 0)
99 printf("## Baudrate may not be deleted\n");
105 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
108 * serial_initfunc() - Forward declare of driver registration routine
109 * @name: Name of the real driver registration routine.
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.
117 #define serial_initfunc(name) \
119 __attribute__((weak, alias("serial_null")));
121 serial_initfunc(atmel_serial_initialize);
122 serial_initfunc(mcf_serial_initialize);
123 serial_initfunc(mpc85xx_serial_initialize);
124 serial_initfunc(mxc_serial_initialize);
125 serial_initfunc(ns16550_serial_initialize);
126 serial_initfunc(pl01x_serial_initialize);
127 serial_initfunc(pxa_serial_initialize);
128 serial_initfunc(sh_serial_initialize);
129 serial_initfunc(mtk_serial_initialize);
132 * serial_register() - Register serial driver with serial driver core
133 * @dev: Pointer to the serial driver structure
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
141 void serial_register(struct serial_device *dev)
143 #ifdef CONFIG_NEEDS_MANUAL_RELOC
145 dev->start += gd->reloc_off;
147 dev->stop += gd->reloc_off;
149 dev->setbrg += gd->reloc_off;
151 dev->getc += gd->reloc_off;
153 dev->tstc += gd->reloc_off;
155 dev->putc += gd->reloc_off;
157 dev->puts += gd->reloc_off;
160 dev->next = serial_devices;
161 serial_devices = dev;
165 * serial_initialize() - Register all compiled-in serial port drivers
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
173 int serial_initialize(void)
175 atmel_serial_initialize();
176 mcf_serial_initialize();
177 mpc85xx_serial_initialize();
178 mxc_serial_initialize();
179 ns16550_serial_initialize();
180 pl01x_serial_initialize();
181 pxa_serial_initialize();
182 sh_serial_initialize();
183 mtk_serial_initialize();
185 serial_assign(default_serial_console()->name);
190 static int serial_stub_start(struct stdio_dev *sdev)
192 struct serial_device *dev = sdev->priv;
197 static int serial_stub_stop(struct stdio_dev *sdev)
199 struct serial_device *dev = sdev->priv;
204 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
206 struct serial_device *dev = sdev->priv;
211 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
213 struct serial_device *dev = sdev->priv;
218 static int serial_stub_getc(struct stdio_dev *sdev)
220 struct serial_device *dev = sdev->priv;
225 static int serial_stub_tstc(struct stdio_dev *sdev)
227 struct serial_device *dev = sdev->priv;
233 * serial_stdio_init() - Register serial ports with STDIO core
235 * This function generates a proxy driver for each serial port driver.
236 * These proxy drivers then register with the STDIO core, making the
237 * serial drivers available as STDIO devices.
239 void serial_stdio_init(void)
241 struct stdio_dev dev;
242 struct serial_device *s = serial_devices;
245 memset(&dev, 0, sizeof(dev));
247 strcpy(dev.name, s->name);
248 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
250 dev.start = serial_stub_start;
251 dev.stop = serial_stub_stop;
252 dev.putc = serial_stub_putc;
253 dev.puts = serial_stub_puts;
254 dev.getc = serial_stub_getc;
255 dev.tstc = serial_stub_tstc;
258 stdio_register(&dev);
265 * serial_assign() - Select the serial output device by name
266 * @name: Name of the serial driver to be used as default output
268 * This function configures the serial output multiplexing by
269 * selecting which serial device will be used as default. In case
270 * the STDIO "serial" device is selected as stdin/stdout/stderr,
271 * the serial device previously configured by this function will be
272 * used for the particular operation.
274 * Returns 0 on success, negative on error.
276 int serial_assign(const char *name)
278 struct serial_device *s;
280 for (s = serial_devices; s; s = s->next) {
281 if (strcmp(s->name, name))
291 * serial_reinit_all() - Reinitialize all compiled-in serial ports
293 * This function reinitializes all serial ports that are compiled
294 * into U-Boot by calling their serial_start() functions.
296 void serial_reinit_all(void)
298 struct serial_device *s;
300 for (s = serial_devices; s; s = s->next)
305 * get_current() - Return pointer to currently selected serial port
307 * This function returns a pointer to currently selected serial port.
308 * The currently selected serial port is altered by serial_assign()
311 * In case this function is called before relocation or before any serial
312 * port is configured, this function calls default_serial_console() to
313 * determine the serial port. Otherwise, the configured serial port is
316 * Returns pointer to the currently selected serial port on success,
319 static struct serial_device *get_current(void)
321 struct serial_device *dev;
323 if (!(gd->flags & GD_FLG_RELOC))
324 dev = default_serial_console();
325 else if (!serial_current)
326 dev = default_serial_console();
328 dev = serial_current;
330 /* We must have a console device */
332 #ifdef CONFIG_SPL_BUILD
333 puts("Cannot find console\n");
336 panic("Cannot find console\n");
344 * serial_init() - Initialize currently selected serial port
346 * This function initializes the currently selected serial port. This
347 * usually involves setting up the registers of that particular port,
348 * enabling clock and such. This function uses the get_current() call
349 * to determine which port is selected.
351 * Returns 0 on success, negative on error.
353 int serial_init(void)
355 gd->flags |= GD_FLG_SERIAL_READY;
356 return get_current()->start();
360 * serial_setbrg() - Configure baud-rate of currently selected serial port
362 * This function configures the baud-rate of the currently selected
363 * serial port. The baud-rate is retrieved from global data within
364 * the serial port driver. This function uses the get_current() call
365 * to determine which port is selected.
367 * Returns 0 on success, negative on error.
369 void serial_setbrg(void)
371 get_current()->setbrg();
375 * serial_getc() - Read character from currently selected serial port
377 * This function retrieves a character from currently selected serial
378 * port. In case there is no character waiting on the serial port,
379 * this function will block and wait for the character to appear. This
380 * function uses the get_current() call to determine which port is
383 * Returns the character on success, negative on error.
385 int serial_getc(void)
387 return get_current()->getc();
391 * serial_tstc() - Test if data is available on currently selected serial port
393 * This function tests if one or more characters are available on
394 * currently selected serial port. This function never blocks. This
395 * function uses the get_current() call to determine which port is
398 * Returns positive if character is available, zero otherwise.
400 int serial_tstc(void)
402 return get_current()->tstc();
406 * serial_putc() - Output character via currently selected serial port
407 * @c: Single character to be output from the serial port.
409 * This function outputs a character via currently selected serial
410 * port. This character is passed to the serial port driver responsible
411 * for controlling the hardware. The hardware may still be in process
412 * of transmitting another character, therefore this function may block
413 * for a short amount of time. This function uses the get_current()
414 * call to determine which port is selected.
416 void serial_putc(const char c)
418 get_current()->putc(c);
422 * serial_puts() - Output string via currently selected serial port
423 * @s: Zero-terminated string to be output from the serial port.
425 * This function outputs a zero-terminated string via currently
426 * selected serial port. This function behaves as an accelerator
427 * in case the hardware can queue multiple characters for transfer.
428 * The whole string that is to be output is available to the function
429 * implementing the hardware manipulation. Transmitting the whole
430 * string may take some time, thus this function may block for some
431 * amount of time. This function uses the get_current() call to
432 * determine which port is selected.
434 void serial_puts(const char *s)
436 get_current()->puts(s);
440 * default_serial_puts() - Output string by calling serial_putc() in loop
441 * @s: Zero-terminated string to be output from the serial port.
443 * This function outputs a zero-terminated string by calling serial_putc()
444 * in a loop. Most drivers do not support queueing more than one byte for
445 * transfer, thus this function precisely implements their serial_puts().
447 * To optimize the number of get_current() calls, this function only
448 * calls get_current() once and then directly accesses the putc() call
449 * of the &struct serial_device .
451 void default_serial_puts(const char *s)
453 struct serial_device *dev = get_current();
458 #if CONFIG_POST & CONFIG_SYS_POST_UART
459 static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
462 * uart_post_test() - Test the currently selected serial port using POST
463 * @flags: POST framework flags
465 * Do a loopback test of the currently selected serial port. This
466 * function is only useful in the context of the POST testing framwork.
467 * The serial port is first configured into loopback mode and then
468 * characters are sent through it.
470 * Returns 0 on success, value otherwise.
472 /* Mark weak until post/cpu/.../uart.c migrate over */
474 int uart_post_test(int flags)
477 int ret, saved_baud, b;
478 struct serial_device *saved_dev, *s;
480 /* Save current serial state */
482 saved_dev = serial_current;
483 saved_baud = gd->baudrate;
485 for (s = serial_devices; s; s = s->next) {
486 /* If this driver doesn't support loop back, skip it */
490 /* Test the next device */
497 /* Consume anything that happens to be queued */
498 while (serial_tstc())
501 /* Enable loop back */
504 /* Test every available baud rate */
505 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
506 gd->baudrate = bauds[b];
510 * Stick to printable chars to avoid issues:
511 * - terminal corruption
512 * - serial program reacting to sequences and sending
513 * back random extra data
514 * - most serial drivers add in extra chars (like \r\n)
516 for (c = 0x20; c < 0x7f; ++c) {
520 /* Make sure it's the same one */
521 ret = (c != serial_getc());
527 /* Clean up the output in case it was sent */
529 ret = ('\b' != serial_getc());
537 /* Disable loop back */
540 /* XXX: There is no serial_stop() !? */
546 /* Restore previous serial state */
547 serial_current = saved_dev;
548 gd->baudrate = saved_baud;