1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 #include <env_internal.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 * @name: changed environment variable
40 * @value: new value of the environment variable
41 * @op: operation (create, overwrite, or delete)
42 * @flags: attributes of environment variable change,
43 * see flags H_* in include/search.h
45 * This will check for a valid baudrate and only apply it if valid.
47 * Return: 0 on success, 1 on error
49 static int on_baudrate(const char *name, const char *value, enum env_op op,
57 case env_op_overwrite:
59 * Switch to new baudrate if new baudrate is supported
61 baudrate = simple_strtoul(value, NULL, 10);
63 /* Not actually changing */
64 if (gd->baudrate == baudrate)
67 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
68 if (baudrate == baudrate_table[i])
71 if (i == ARRAY_SIZE(baudrate_table)) {
72 if ((flags & H_FORCE) == 0)
73 printf("## Baudrate %d bps not supported\n",
77 if ((flags & H_INTERACTIVE) != 0) {
78 printf("## Switch baudrate to %d"
79 " bps and press ENTER ...\n", baudrate);
83 gd->baudrate = baudrate;
89 if ((flags & H_INTERACTIVE) != 0)
97 printf("## Baudrate may not be deleted\n");
103 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
106 * serial_initfunc() - Forward declare of driver registration routine
107 * @name: Name of the real driver registration routine.
109 * This macro expands onto forward declaration of a driver registration
110 * routine, which is then used below in serial_initialize() function.
111 * The declaration is made weak and aliases to serial_null() so in case
112 * the driver is not compiled in, the function is still declared and can
113 * be used, but aliases to serial_null() and thus is optimized away.
115 #define serial_initfunc(name) \
117 __attribute__((weak, alias("serial_null")));
119 serial_initfunc(atmel_serial_initialize);
120 serial_initfunc(mcf_serial_initialize);
121 serial_initfunc(mpc85xx_serial_initialize);
122 serial_initfunc(mxc_serial_initialize);
123 serial_initfunc(ns16550_serial_initialize);
124 serial_initfunc(pl01x_serial_initialize);
125 serial_initfunc(pxa_serial_initialize);
126 serial_initfunc(sh_serial_initialize);
129 * serial_register() - Register serial driver with serial driver core
130 * @dev: Pointer to the serial driver structure
132 * This function registers the serial driver supplied via @dev with
133 * serial driver core, thus making U-Boot aware of it and making it
134 * available for U-Boot to use. On platforms that still require manual
135 * relocation of constant variables, relocation of the supplied structure
138 void serial_register(struct serial_device *dev)
140 #ifdef CONFIG_NEEDS_MANUAL_RELOC
142 dev->start += gd->reloc_off;
144 dev->stop += gd->reloc_off;
146 dev->setbrg += gd->reloc_off;
148 dev->getc += gd->reloc_off;
150 dev->tstc += gd->reloc_off;
152 dev->putc += gd->reloc_off;
154 dev->puts += gd->reloc_off;
157 dev->next = serial_devices;
158 serial_devices = dev;
162 * serial_initialize() - Register all compiled-in serial port drivers
164 * This function registers all serial port drivers that are compiled
165 * into the U-Boot binary with the serial core, thus making them
166 * available to U-Boot to use. Lastly, this function assigns a default
167 * serial port to the serial core. That serial port is then used as a
170 void serial_initialize(void)
172 atmel_serial_initialize();
173 mcf_serial_initialize();
174 mpc85xx_serial_initialize();
175 mxc_serial_initialize();
176 ns16550_serial_initialize();
177 pl01x_serial_initialize();
178 pxa_serial_initialize();
179 sh_serial_initialize();
181 serial_assign(default_serial_console()->name);
184 static int serial_stub_start(struct stdio_dev *sdev)
186 struct serial_device *dev = sdev->priv;
191 static int serial_stub_stop(struct stdio_dev *sdev)
193 struct serial_device *dev = sdev->priv;
198 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
200 struct serial_device *dev = sdev->priv;
205 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
207 struct serial_device *dev = sdev->priv;
212 static int serial_stub_getc(struct stdio_dev *sdev)
214 struct serial_device *dev = sdev->priv;
219 static int serial_stub_tstc(struct stdio_dev *sdev)
221 struct serial_device *dev = sdev->priv;
227 * serial_stdio_init() - Register serial ports with STDIO core
229 * This function generates a proxy driver for each serial port driver.
230 * These proxy drivers then register with the STDIO core, making the
231 * serial drivers available as STDIO devices.
233 void serial_stdio_init(void)
235 struct stdio_dev dev;
236 struct serial_device *s = serial_devices;
239 memset(&dev, 0, sizeof(dev));
241 strcpy(dev.name, s->name);
242 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
244 dev.start = serial_stub_start;
245 dev.stop = serial_stub_stop;
246 dev.putc = serial_stub_putc;
247 dev.puts = serial_stub_puts;
248 dev.getc = serial_stub_getc;
249 dev.tstc = serial_stub_tstc;
252 stdio_register(&dev);
259 * serial_assign() - Select the serial output device by name
260 * @name: Name of the serial driver to be used as default output
262 * This function configures the serial output multiplexing by
263 * selecting which serial device will be used as default. In case
264 * the STDIO "serial" device is selected as stdin/stdout/stderr,
265 * the serial device previously configured by this function will be
266 * used for the particular operation.
268 * Returns 0 on success, negative on error.
270 int serial_assign(const char *name)
272 struct serial_device *s;
274 for (s = serial_devices; s; s = s->next) {
275 if (strcmp(s->name, name))
285 * serial_reinit_all() - Reinitialize all compiled-in serial ports
287 * This function reinitializes all serial ports that are compiled
288 * into U-Boot by calling their serial_start() functions.
290 void serial_reinit_all(void)
292 struct serial_device *s;
294 for (s = serial_devices; s; s = s->next)
299 * get_current() - Return pointer to currently selected serial port
301 * This function returns a pointer to currently selected serial port.
302 * The currently selected serial port is altered by serial_assign()
305 * In case this function is called before relocation or before any serial
306 * port is configured, this function calls default_serial_console() to
307 * determine the serial port. Otherwise, the configured serial port is
310 * Returns pointer to the currently selected serial port on success,
313 static struct serial_device *get_current(void)
315 struct serial_device *dev;
317 if (!(gd->flags & GD_FLG_RELOC))
318 dev = default_serial_console();
319 else if (!serial_current)
320 dev = default_serial_console();
322 dev = serial_current;
324 /* We must have a console device */
326 #ifdef CONFIG_SPL_BUILD
327 puts("Cannot find console\n");
330 panic("Cannot find console\n");
338 * serial_init() - Initialize currently selected serial port
340 * This function initializes the currently selected serial port. This
341 * usually involves setting up the registers of that particular port,
342 * enabling clock and such. This function uses the get_current() call
343 * to determine which port is selected.
345 * Returns 0 on success, negative on error.
347 int serial_init(void)
349 gd->flags |= GD_FLG_SERIAL_READY;
350 return get_current()->start();
354 * serial_setbrg() - Configure baud-rate of currently selected serial port
356 * This function configures the baud-rate of the currently selected
357 * serial port. The baud-rate is retrieved from global data within
358 * the serial port driver. This function uses the get_current() call
359 * to determine which port is selected.
361 * Returns 0 on success, negative on error.
363 void serial_setbrg(void)
365 get_current()->setbrg();
369 * serial_getc() - Read character from currently selected serial port
371 * This function retrieves a character from currently selected serial
372 * port. In case there is no character waiting on the serial port,
373 * this function will block and wait for the character to appear. This
374 * function uses the get_current() call to determine which port is
377 * Returns the character on success, negative on error.
379 int serial_getc(void)
381 return get_current()->getc();
385 * serial_tstc() - Test if data is available on currently selected serial port
387 * This function tests if one or more characters are available on
388 * currently selected serial port. This function never blocks. This
389 * function uses the get_current() call to determine which port is
392 * Returns positive if character is available, zero otherwise.
394 int serial_tstc(void)
396 return get_current()->tstc();
400 * serial_putc() - Output character via currently selected serial port
401 * @c: Single character to be output from the serial port.
403 * This function outputs a character via currently selected serial
404 * port. This character is passed to the serial port driver responsible
405 * for controlling the hardware. The hardware may still be in process
406 * of transmitting another character, therefore this function may block
407 * for a short amount of time. This function uses the get_current()
408 * call to determine which port is selected.
410 void serial_putc(const char c)
412 get_current()->putc(c);
416 * serial_puts() - Output string via currently selected serial port
417 * @s: Zero-terminated string to be output from the serial port.
419 * This function outputs a zero-terminated string via currently
420 * selected serial port. This function behaves as an accelerator
421 * in case the hardware can queue multiple characters for transfer.
422 * The whole string that is to be output is available to the function
423 * implementing the hardware manipulation. Transmitting the whole
424 * string may take some time, thus this function may block for some
425 * amount of time. This function uses the get_current() call to
426 * determine which port is selected.
428 void serial_puts(const char *s)
430 get_current()->puts(s);
434 * default_serial_puts() - Output string by calling serial_putc() in loop
435 * @s: Zero-terminated string to be output from the serial port.
437 * This function outputs a zero-terminated string by calling serial_putc()
438 * in a loop. Most drivers do not support queueing more than one byte for
439 * transfer, thus this function precisely implements their serial_puts().
441 * To optimize the number of get_current() calls, this function only
442 * calls get_current() once and then directly accesses the putc() call
443 * of the &struct serial_device .
445 void default_serial_puts(const char *s)
447 struct serial_device *dev = get_current();
452 #if CONFIG_POST & CONFIG_SYS_POST_UART
453 static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
456 * uart_post_test() - Test the currently selected serial port using POST
457 * @flags: POST framework flags
459 * Do a loopback test of the currently selected serial port. This
460 * function is only useful in the context of the POST testing framwork.
461 * The serial port is first configured into loopback mode and then
462 * characters are sent through it.
464 * Returns 0 on success, value otherwise.
466 /* Mark weak until post/cpu/.../uart.c migrate over */
468 int uart_post_test(int flags)
471 int ret, saved_baud, b;
472 struct serial_device *saved_dev, *s;
474 /* Save current serial state */
476 saved_dev = serial_current;
477 saved_baud = gd->baudrate;
479 for (s = serial_devices; s; s = s->next) {
480 /* If this driver doesn't support loop back, skip it */
484 /* Test the next device */
491 /* Consume anything that happens to be queued */
492 while (serial_tstc())
495 /* Enable loop back */
498 /* Test every available baud rate */
499 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
500 gd->baudrate = bauds[b];
504 * Stick to printable chars to avoid issues:
505 * - terminal corruption
506 * - serial program reacting to sequences and sending
507 * back random extra data
508 * - most serial drivers add in extra chars (like \r\n)
510 for (c = 0x20; c < 0x7f; ++c) {
514 /* Make sure it's the same one */
515 ret = (c != serial_getc());
521 /* Clean up the output in case it was sent */
523 ret = ('\b' != serial_getc());
531 /* Disable loop back */
534 /* XXX: There is no serial_stop() !? */
540 /* Restore previous serial state */
541 serial_current = saved_dev;
542 gd->baudrate = saved_baud;