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 <asm/global_data.h>
14 #include <linux/compiler.h>
16 #include <linux/delay.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 static struct serial_device *serial_devices;
21 static struct serial_device *serial_current;
23 * Table with supported baudrates (defined in config_xyz.h)
25 static const unsigned long baudrate_table[] = CFG_SYS_BAUDRATE_TABLE;
28 * serial_null() - Void registration routine of a serial driver
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
35 static void serial_null(void)
40 * on_baudrate() - Update the actual baudrate when the env var changes
42 * @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
48 * This will check for a valid baudrate and only apply it if valid.
50 * Return: 0 on success, 1 on error
52 static int on_baudrate(const char *name, const char *value, enum env_op op,
60 case env_op_overwrite:
62 * Switch to new baudrate if new baudrate is supported
64 baudrate = dectoul(value, NULL);
66 /* Not actually changing */
67 if (gd->baudrate == baudrate)
70 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
71 if (baudrate == baudrate_table[i])
74 if (i == ARRAY_SIZE(baudrate_table)) {
75 if ((flags & H_FORCE) == 0)
76 printf("## Baudrate %d bps not supported\n",
80 if ((flags & H_INTERACTIVE) != 0) {
81 printf("## Switch baudrate to %d"
82 " bps and press ENTER ...\n", baudrate);
86 gd->baudrate = baudrate;
92 if ((flags & H_INTERACTIVE) != 0)
94 if (getchar() == '\r')
100 printf("## Baudrate may not be deleted\n");
106 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
109 * serial_initfunc() - Forward declare of driver registration routine
110 * @name: Name of the real driver registration routine.
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.
118 #define serial_initfunc(name) \
120 __attribute__((weak, alias("serial_null")));
122 serial_initfunc(atmel_serial_initialize);
123 serial_initfunc(mcf_serial_initialize);
124 serial_initfunc(mpc85xx_serial_initialize);
125 serial_initfunc(mxc_serial_initialize);
126 serial_initfunc(ns16550_serial_initialize);
127 serial_initfunc(pl01x_serial_initialize);
128 serial_initfunc(pxa_serial_initialize);
129 serial_initfunc(smh_serial_initialize);
130 serial_initfunc(sh_serial_initialize);
131 serial_initfunc(mtk_serial_initialize);
134 * serial_register() - Register serial driver with serial driver core
135 * @dev: Pointer to the serial driver structure
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
143 void serial_register(struct serial_device *dev)
145 #ifdef CONFIG_NEEDS_MANUAL_RELOC
147 dev->start += gd->reloc_off;
149 dev->stop += gd->reloc_off;
151 dev->setbrg += gd->reloc_off;
153 dev->getc += gd->reloc_off;
155 dev->tstc += gd->reloc_off;
157 dev->putc += gd->reloc_off;
159 dev->puts += gd->reloc_off;
162 dev->next = serial_devices;
163 serial_devices = dev;
167 * serial_initialize() - Register all compiled-in serial port drivers
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
175 int serial_initialize(void)
177 atmel_serial_initialize();
178 mcf_serial_initialize();
179 mpc85xx_serial_initialize();
180 mxc_serial_initialize();
181 ns16550_serial_initialize();
182 pl01x_serial_initialize();
183 pxa_serial_initialize();
184 smh_serial_initialize();
185 sh_serial_initialize();
186 mtk_serial_initialize();
188 serial_assign(default_serial_console()->name);
193 static int serial_stub_start(struct stdio_dev *sdev)
195 struct serial_device *dev = sdev->priv;
200 static int serial_stub_stop(struct stdio_dev *sdev)
202 struct serial_device *dev = sdev->priv;
207 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
209 struct serial_device *dev = sdev->priv;
214 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
216 struct serial_device *dev = sdev->priv;
221 static int serial_stub_getc(struct stdio_dev *sdev)
223 struct serial_device *dev = sdev->priv;
228 static int serial_stub_tstc(struct stdio_dev *sdev)
230 struct serial_device *dev = sdev->priv;
236 * serial_stdio_init() - Register serial ports with STDIO core
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.
242 void serial_stdio_init(void)
244 struct stdio_dev dev;
245 struct serial_device *s = serial_devices;
248 memset(&dev, 0, sizeof(dev));
250 strcpy(dev.name, s->name);
251 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
253 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;
261 stdio_register(&dev);
268 * serial_assign() - Select the serial output device by name
269 * @name: Name of the serial driver to be used as default output
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.
277 * Returns 0 on success, negative on error.
279 int serial_assign(const char *name)
281 struct serial_device *s;
283 for (s = serial_devices; s; s = s->next) {
284 if (strcmp(s->name, name))
294 * serial_reinit_all() - Reinitialize all compiled-in serial ports
296 * This function reinitializes all serial ports that are compiled
297 * into U-Boot by calling their serial_start() functions.
299 void serial_reinit_all(void)
301 struct serial_device *s;
303 for (s = serial_devices; s; s = s->next)
308 * get_current() - Return pointer to currently selected serial port
310 * This function returns a pointer to currently selected serial port.
311 * The currently selected serial port is altered by serial_assign()
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
319 * Returns pointer to the currently selected serial port on success,
322 static struct serial_device *get_current(void)
324 struct serial_device *dev;
326 if (!(gd->flags & GD_FLG_RELOC))
327 dev = default_serial_console();
328 else if (!serial_current)
329 dev = default_serial_console();
331 dev = serial_current;
333 /* We must have a console device */
335 #ifdef CONFIG_SPL_BUILD
336 puts("Cannot find console\n");
339 panic("Cannot find console\n");
347 * serial_init() - Initialize currently selected serial port
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.
354 * Returns 0 on success, negative on error.
356 int serial_init(void)
358 gd->flags |= GD_FLG_SERIAL_READY;
359 return get_current()->start();
363 * serial_setbrg() - Configure baud-rate of currently selected serial port
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.
370 * Returns 0 on success, negative on error.
372 void serial_setbrg(void)
374 get_current()->setbrg();
378 * serial_getc() - Read character from currently selected serial port
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
386 * Returns the character on success, negative on error.
388 int serial_getc(void)
390 return get_current()->getc();
394 * serial_tstc() - Test if data is available on currently selected serial port
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
401 * Returns positive if character is available, zero otherwise.
403 int serial_tstc(void)
405 return get_current()->tstc();
409 * serial_putc() - Output character via currently selected serial port
410 * @c: Single character to be output from the serial port.
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.
419 void serial_putc(const char c)
421 get_current()->putc(c);
425 * serial_puts() - Output string via currently selected serial port
426 * @s: Zero-terminated string to be output from the serial port.
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.
437 void serial_puts(const char *s)
439 get_current()->puts(s);
443 * default_serial_puts() - Output string by calling serial_putc() in loop
444 * @s: Zero-terminated string to be output from the serial port.
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().
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 .
454 void default_serial_puts(const char *s)
456 struct serial_device *dev = get_current();
461 #if CFG_POST & CFG_SYS_POST_UART
462 static const int bauds[] = CFG_SYS_BAUDRATE_TABLE;
465 * uart_post_test() - Test the currently selected serial port using POST
466 * @flags: POST framework flags
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.
470 * The serial port is first configured into loopback mode and then
471 * characters are sent through it.
473 * Returns 0 on success, value otherwise.
475 /* Mark weak until post/cpu/.../uart.c migrate over */
477 int uart_post_test(int flags)
480 int ret, saved_baud, b;
481 struct serial_device *saved_dev, *s;
483 /* Save current serial state */
485 saved_dev = serial_current;
486 saved_baud = gd->baudrate;
488 for (s = serial_devices; s; s = s->next) {
489 /* If this driver doesn't support loop back, skip it */
493 /* Test the next device */
500 /* Consume anything that happens to be queued */
501 while (serial_tstc())
504 /* Enable loop back */
507 /* Test every available baud rate */
508 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
509 gd->baudrate = bauds[b];
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)
519 for (c = 0x20; c < 0x7f; ++c) {
523 /* Make sure it's the same one */
524 ret = (c != serial_getc());
530 /* Clean up the output in case it was sent */
532 ret = ('\b' != serial_getc());
540 /* Disable loop back */
543 /* XXX: There is no serial_stop() !? */
549 /* Restore previous serial state */
550 serial_current = saved_dev;
551 gd->baudrate = saved_baud;