1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 #include <environment.h>
10 #include <stdio_dev.h>
12 #include <linux/compiler.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 static struct serial_device *serial_devices;
18 static struct serial_device *serial_current;
20 * Table with supported baudrates (defined in config_xyz.h)
22 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
25 * serial_null() - Void registration routine of a serial driver
27 * This routine implements a void registration routine of a serial
28 * driver. The registration routine of a particular driver is aliased
29 * to this empty function in case the driver is not compiled into
32 static void serial_null(void)
37 * on_baudrate() - Update the actual baudrate when the env var changes
39 * This will check for a valid baudrate and only apply it if valid.
41 static int on_baudrate(const char *name, const char *value, enum env_op op,
49 case env_op_overwrite:
51 * Switch to new baudrate if new baudrate is supported
53 baudrate = simple_strtoul(value, NULL, 10);
55 /* Not actually changing */
56 if (gd->baudrate == baudrate)
59 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
60 if (baudrate == baudrate_table[i])
63 if (i == ARRAY_SIZE(baudrate_table)) {
64 if ((flags & H_FORCE) == 0)
65 printf("## Baudrate %d bps not supported\n",
69 if ((flags & H_INTERACTIVE) != 0) {
70 printf("## Switch baudrate to %d"
71 " bps and press ENTER ...\n", baudrate);
75 gd->baudrate = baudrate;
81 if ((flags & H_INTERACTIVE) != 0)
89 printf("## Baudrate may not be deleted\n");
95 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
98 * serial_initfunc() - Forward declare of driver registration routine
99 * @name: Name of the real driver registration routine.
101 * This macro expands onto forward declaration of a driver registration
102 * routine, which is then used below in serial_initialize() function.
103 * The declaration is made weak and aliases to serial_null() so in case
104 * the driver is not compiled in, the function is still declared and can
105 * be used, but aliases to serial_null() and thus is optimized away.
107 #define serial_initfunc(name) \
109 __attribute__((weak, alias("serial_null")));
111 serial_initfunc(atmel_serial_initialize);
112 serial_initfunc(mcf_serial_initialize);
113 serial_initfunc(mpc85xx_serial_initialize);
114 serial_initfunc(mpc8xx_serial_initialize);
115 serial_initfunc(mxc_serial_initialize);
116 serial_initfunc(ns16550_serial_initialize);
117 serial_initfunc(pl01x_serial_initialize);
118 serial_initfunc(pxa_serial_initialize);
119 serial_initfunc(sh_serial_initialize);
122 * serial_register() - Register serial driver with serial driver core
123 * @dev: Pointer to the serial driver structure
125 * This function registers the serial driver supplied via @dev with
126 * serial driver core, thus making U-Boot aware of it and making it
127 * available for U-Boot to use. On platforms that still require manual
128 * relocation of constant variables, relocation of the supplied structure
131 void serial_register(struct serial_device *dev)
133 #ifdef CONFIG_NEEDS_MANUAL_RELOC
135 dev->start += gd->reloc_off;
137 dev->stop += gd->reloc_off;
139 dev->setbrg += gd->reloc_off;
141 dev->getc += gd->reloc_off;
143 dev->tstc += gd->reloc_off;
145 dev->putc += gd->reloc_off;
147 dev->puts += gd->reloc_off;
150 dev->next = serial_devices;
151 serial_devices = dev;
155 * serial_initialize() - Register all compiled-in serial port drivers
157 * This function registers all serial port drivers that are compiled
158 * into the U-Boot binary with the serial core, thus making them
159 * available to U-Boot to use. Lastly, this function assigns a default
160 * serial port to the serial core. That serial port is then used as a
163 void serial_initialize(void)
165 atmel_serial_initialize();
166 mcf_serial_initialize();
167 mpc85xx_serial_initialize();
168 mpc8xx_serial_initialize();
169 mxc_serial_initialize();
170 ns16550_serial_initialize();
171 pl01x_serial_initialize();
172 pxa_serial_initialize();
173 sh_serial_initialize();
175 serial_assign(default_serial_console()->name);
178 static int serial_stub_start(struct stdio_dev *sdev)
180 struct serial_device *dev = sdev->priv;
185 static int serial_stub_stop(struct stdio_dev *sdev)
187 struct serial_device *dev = sdev->priv;
192 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
194 struct serial_device *dev = sdev->priv;
199 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
201 struct serial_device *dev = sdev->priv;
206 static int serial_stub_getc(struct stdio_dev *sdev)
208 struct serial_device *dev = sdev->priv;
213 static int serial_stub_tstc(struct stdio_dev *sdev)
215 struct serial_device *dev = sdev->priv;
221 * serial_stdio_init() - Register serial ports with STDIO core
223 * This function generates a proxy driver for each serial port driver.
224 * These proxy drivers then register with the STDIO core, making the
225 * serial drivers available as STDIO devices.
227 void serial_stdio_init(void)
229 struct stdio_dev dev;
230 struct serial_device *s = serial_devices;
233 memset(&dev, 0, sizeof(dev));
235 strcpy(dev.name, s->name);
236 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
238 dev.start = serial_stub_start;
239 dev.stop = serial_stub_stop;
240 dev.putc = serial_stub_putc;
241 dev.puts = serial_stub_puts;
242 dev.getc = serial_stub_getc;
243 dev.tstc = serial_stub_tstc;
246 stdio_register(&dev);
253 * serial_assign() - Select the serial output device by name
254 * @name: Name of the serial driver to be used as default output
256 * This function configures the serial output multiplexing by
257 * selecting which serial device will be used as default. In case
258 * the STDIO "serial" device is selected as stdin/stdout/stderr,
259 * the serial device previously configured by this function will be
260 * used for the particular operation.
262 * Returns 0 on success, negative on error.
264 int serial_assign(const char *name)
266 struct serial_device *s;
268 for (s = serial_devices; s; s = s->next) {
269 if (strcmp(s->name, name))
279 * serial_reinit_all() - Reinitialize all compiled-in serial ports
281 * This function reinitializes all serial ports that are compiled
282 * into U-Boot by calling their serial_start() functions.
284 void serial_reinit_all(void)
286 struct serial_device *s;
288 for (s = serial_devices; s; s = s->next)
293 * get_current() - Return pointer to currently selected serial port
295 * This function returns a pointer to currently selected serial port.
296 * The currently selected serial port is altered by serial_assign()
299 * In case this function is called before relocation or before any serial
300 * port is configured, this function calls default_serial_console() to
301 * determine the serial port. Otherwise, the configured serial port is
304 * Returns pointer to the currently selected serial port on success,
307 static struct serial_device *get_current(void)
309 struct serial_device *dev;
311 if (!(gd->flags & GD_FLG_RELOC))
312 dev = default_serial_console();
313 else if (!serial_current)
314 dev = default_serial_console();
316 dev = serial_current;
318 /* We must have a console device */
320 #ifdef CONFIG_SPL_BUILD
321 puts("Cannot find console\n");
324 panic("Cannot find console\n");
332 * serial_init() - Initialize currently selected serial port
334 * This function initializes the currently selected serial port. This
335 * usually involves setting up the registers of that particular port,
336 * enabling clock and such. This function uses the get_current() call
337 * to determine which port is selected.
339 * Returns 0 on success, negative on error.
341 int serial_init(void)
343 gd->flags |= GD_FLG_SERIAL_READY;
344 return get_current()->start();
348 * serial_setbrg() - Configure baud-rate of currently selected serial port
350 * This function configures the baud-rate of the currently selected
351 * serial port. The baud-rate is retrieved from global data within
352 * the serial port driver. This function uses the get_current() call
353 * to determine which port is selected.
355 * Returns 0 on success, negative on error.
357 void serial_setbrg(void)
359 get_current()->setbrg();
363 * serial_getc() - Read character from currently selected serial port
365 * This function retrieves a character from currently selected serial
366 * port. In case there is no character waiting on the serial port,
367 * this function will block and wait for the character to appear. This
368 * function uses the get_current() call to determine which port is
371 * Returns the character on success, negative on error.
373 int serial_getc(void)
375 return get_current()->getc();
379 * serial_tstc() - Test if data is available on currently selected serial port
381 * This function tests if one or more characters are available on
382 * currently selected serial port. This function never blocks. This
383 * function uses the get_current() call to determine which port is
386 * Returns positive if character is available, zero otherwise.
388 int serial_tstc(void)
390 return get_current()->tstc();
394 * serial_putc() - Output character via currently selected serial port
395 * @c: Single character to be output from the serial port.
397 * This function outputs a character via currently selected serial
398 * port. This character is passed to the serial port driver responsible
399 * for controlling the hardware. The hardware may still be in process
400 * of transmitting another character, therefore this function may block
401 * for a short amount of time. This function uses the get_current()
402 * call to determine which port is selected.
404 void serial_putc(const char c)
406 get_current()->putc(c);
410 * serial_puts() - Output string via currently selected serial port
411 * @s: Zero-terminated string to be output from the serial port.
413 * This function outputs a zero-terminated string via currently
414 * selected serial port. This function behaves as an accelerator
415 * in case the hardware can queue multiple characters for transfer.
416 * The whole string that is to be output is available to the function
417 * implementing the hardware manipulation. Transmitting the whole
418 * string may take some time, thus this function may block for some
419 * amount of time. This function uses the get_current() call to
420 * determine which port is selected.
422 void serial_puts(const char *s)
424 get_current()->puts(s);
428 * default_serial_puts() - Output string by calling serial_putc() in loop
429 * @s: Zero-terminated string to be output from the serial port.
431 * This function outputs a zero-terminated string by calling serial_putc()
432 * in a loop. Most drivers do not support queueing more than one byte for
433 * transfer, thus this function precisely implements their serial_puts().
435 * To optimize the number of get_current() calls, this function only
436 * calls get_current() once and then directly accesses the putc() call
437 * of the &struct serial_device .
439 void default_serial_puts(const char *s)
441 struct serial_device *dev = get_current();
446 #if CONFIG_POST & CONFIG_SYS_POST_UART
447 static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
450 * uart_post_test() - Test the currently selected serial port using POST
451 * @flags: POST framework flags
453 * Do a loopback test of the currently selected serial port. This
454 * function is only useful in the context of the POST testing framwork.
455 * The serial port is first configured into loopback mode and then
456 * characters are sent through it.
458 * Returns 0 on success, value otherwise.
460 /* Mark weak until post/cpu/.../uart.c migrate over */
462 int uart_post_test(int flags)
465 int ret, saved_baud, b;
466 struct serial_device *saved_dev, *s;
468 /* Save current serial state */
470 saved_dev = serial_current;
471 saved_baud = gd->baudrate;
473 for (s = serial_devices; s; s = s->next) {
474 /* If this driver doesn't support loop back, skip it */
478 /* Test the next device */
485 /* Consume anything that happens to be queued */
486 while (serial_tstc())
489 /* Enable loop back */
492 /* Test every available baud rate */
493 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
494 gd->baudrate = bauds[b];
498 * Stick to printable chars to avoid issues:
499 * - terminal corruption
500 * - serial program reacting to sequences and sending
501 * back random extra data
502 * - most serial drivers add in extra chars (like \r\n)
504 for (c = 0x20; c < 0x7f; ++c) {
508 /* Make sure it's the same one */
509 ret = (c != serial_getc());
515 /* Clean up the output in case it was sent */
517 ret = ('\b' != serial_getc());
525 /* Disable loop back */
528 /* XXX: There is no serial_stop() !? */
534 /* Restore previous serial state */
535 serial_current = saved_dev;
536 gd->baudrate = saved_baud;